package com.tjresearch.redisdbtasktracker.service;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tjresearch.redisdbtasktracker.Mapper.JobMapper;
import com.tjresearch.redisdbtasktracker.pojo.BuildMain;
import com.tjresearch.redisdbtasktracker.pojo.HfData;
import com.tjresearch.redisdbtasktracker.pojo.Rate;


@Service
public class JobService {
	@Autowired
	private JobMapper jobMapper;
	@Autowired
	//private JobFileLog jobFileLog;
	
	public static Date date ;
	/**
	 * 将数据插入数据库,原始数据
	 * @param hfData
	 */
	public void insertSourceData(HfData hfData){
		jobMapper.InsertSourceData(hfData);
		//jobFileLog.InsertSourceDataFile(hfData,date);
		
	}
	public void insertRedisLog(String strData){
		try{
		//	jobFileLog.writeLog(strData,date);
		}catch(Exception e){
			new RuntimeException();
		}
		
	}
	/**
	 * 插入加工后的数据
	 * @param hfData
	 */
	public void InsertDailyData(HfData hfData) {
		
		
		jobMapper.InsertDailyData(hfData);	
		//jobFileLog.InsertDailyDataFile(hfData,date);
		
	}
	/**
	 * 查询所有数据
	 * @return
	 */
	public Map<String,HfData> findAllDataFinal(){
		List<HfData> dataFinal = jobMapper.findAllDataFinal();
		Map<String,HfData> mapFinal = new LinkedHashMap<String,HfData>();
		for (HfData data : dataFinal) {
			mapFinal.put(""+data.getItem_id()+"_"+data.getHdate().getTime(), data);
		}
		return mapFinal;
	}
	/**
	 * 将同比结果插入数据库
	 * @param rate
	 */
	public void insertSRate(Rate rate) {
		jobMapper.insertSRate(rate);
		//jobFileLog.InsertDataFile(rate,date);
		
	}
	/**
	 * 将定基比数据插入
	 * @param rate
	 */
	public void insertFRate(Rate rate) {
		jobMapper.insertSRate(rate);
		//jobFileLog.InsertDataFile(rate,date);
	}
	/**
	 * 将环比数据插入
	 * @param rate
	 */
	public void insertRrate(Rate rate) {
		jobMapper.insertSRate(rate);
		//jobFileLog.InsertDataFile(rate,date);
	}
	/**
	 * 通过Item_id和date查询数据
	 * @param item_id
	 * @param date
	 * @return
	 */
	public HfData getDataByItemAndDate(int item_id, Date date) {
		return 	jobMapper.findDataByItemAndDate(item_id,date);
	}
	/**
	 * 通过日期计算期间数据的和
	 * @param item_id
	 * @param date
	 * @param startCalTime
	 * @return
	 */
	public double calRValue(int item_id,Date date, Date startCalTime) {
		return jobMapper.calRValue(item_id,date,startCalTime);
		
	}
	public List<Integer> findAllSourceItemId() {
		
		return jobMapper.findAllSourceItemId();
	}
	
	public Integer findRateIdByIdType(Integer souItemId, int i) {
		return jobMapper.findRateIdByIdType(souItemId,i);
	}

	public String findFillConditionByItemid(Integer item_id)
	{
		return jobMapper.findFillConditionByItemid(item_id);
	}
	
	public boolean shouldIfill(String fillCondition)
	{
		if(!fillCondition.equals("等于"))
		{
			return true;
		}else{
			return false;
		}
	}
	public int translateToDailyId(Integer item_id) {
		return jobMapper.translateToDailyId(item_id);
		
	}
	public void batchInsertSourceData(List<HfData> aRawSeries) {
		jobMapper.batchInsertSourceData(aRawSeries);
		
	}
	public void batchInsertDailyData(List<HfData> aRawSeries) {
		jobMapper.batchInsertDailyData(aRawSeries);
		
	}
	public void batchInsertSRate(List<Rate> aRateList) {
		jobMapper.batchInsertSRate(aRateList);
		
	}
	
	public void truncateHighfreqData()
	{
		jobMapper.truncateHighfreqData();
	}
	public void v7Function1InitDailyData() {
		jobMapper.deleteV7Function1DailyData();
		jobMapper.insertV7Function1DailyData();
		
	}
	public void v7Function2InitDailyData() {
		jobMapper.deleteV7Function2DailyData();
		jobMapper.insertV7Function2DailyData();
	}
	public List<HfData> findDataByDataId(int i) {
		return jobMapper.findDataByItemId(i);
	}
	public void deleteBuildInDaily() {
		jobMapper.deleteBuildInDaily();
		
	}
	public void truncateMainFill() {
		jobMapper.truncateMainFill();	
	}
	public void batchInsertBuildFill(List<BuildMain> mainFillDataList) {
		jobMapper.batchInsertBuildFill(mainFillDataList);
	}
	public void truncateSourceData() {
		jobMapper.truncateSourceData();
		
	}
	public void truncateDailyData() {
		jobMapper.truncateDailyData();
		
	}


}
